<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JSON</title>
    <script>
        var item = [{
            "children": [{
                "children": [{
                    "children": [],
                    "demo": "c1-1-1"
                }],
                "demo": "c1-1"
            }],
            "demo": "c1"
        }, {
            "children": [{
                "children": [],
                "demo": "c2-2-1"
            }, {
                "children": [],
                "demo": "c2-2-2"
            }],
            "demo": "2"
        }, {
            "children": [],
            "demo": "3"
        }];
        console.log(item);
    </script>
</head>

<body>
    <button onclick="delChildren(item)">删除子项</button>
    <script>
        // function delChildren(json) {
        //     for (var i = 0; i < json.length; i++) {
        //         for (var obj in json[i]) {
        //             if (obj == "children") {
        //                 var oChild = json[i].children;
        //                 if (oChild.length === 0) {
        //                     delete json[i].children;
        //                 } else {
        //                     delChildren(oChild);
        //                 }
        //             }
        //             json[i]["key" + i] = '';
        //         }

        //     }
        //     return json;
        // }
        delChildren = (json) => {　　
            for (var i = 0; i < json.length; i++) {
                json[i]["happyKey"] = + +i; //加此处，delete失效
                for (var obj in json[i]) {　　　　　　
                    if (obj == "children") {　　　　　　　　
                        var oChild = json[i].children;　　　　　　　　
                        if (oChild.length === 0) {　　　　　　　　　　
                            delete json[i].children;　　　　　　　　
                        } else {　　　　　　　　　　
                            this.delChildren(oChild);　　　　　　　　
                        }　　　　　　
                    }
                    json[i]["happyKey"] = uuid(5);　　　
                }　
            }　　
            return json;
        }

        function uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [],
                i;
            radix = radix || chars.length;
            if (len) {
                for (i = 0; i < len; i++)
                    uuid[i] = chars[0 | Math.random() * radix];
            } else {
                var r;
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random() * 16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }
            return uuid.join(
                '');
        }
        console.log(delChildren(item));
        console.log("uuid", uuid(2));
    </script>
</body>

</html>